<?php

class RoleController extends AtomController {

	public function filters() {
		return array(
			array('application.filters.RbacExFilter - edit'),
		);
	}

	public function actionEdit($id = null) {
		$id = ($id !== null) ? $id : $_GET['id'];
		$perms = Permission::model()->findAll();
		$role = Role::model()->findByPk($id);

		if (Yii::app()->request->isPostRequest) {
			if(isset($_POST['RolePermissions']))
			{

				foreach($_POST['RolePermissions'] as $k => $v) {
					$perms &= ~ (float)(1 << (int)$k);
					$perms |= (float)(1 << (int)$k);

				}

				$role->base_permissions = $perms;
				if(isset($_POST['submitRolePermissions']))
					if ($role->save()) {
						Yii::app()->user->setFlash('permissionsSaved', 'Role\'s base permissions has been saved');
						$this->redirect(array('edit','id'=>$role->id));
					}
					else
						$this->addErrors($role->getErrors());
			}
		}
		$this->render('base_permissions', array('role'=>$role, 'perms' => $perms));
	}


}

?>
